Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #1552

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Solution #1552

wants to merge 5 commits into from

Conversation

nataliia2211
Copy link

Copy link

@Krrampuss Krrampuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!!!!

Copy link

@VolodymyrKirichenko VolodymyrKirichenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, but review my comments and fix them for all the cases 🥹

src/App.tsx Outdated
Comment on lines 34 to 42
(async () => {
try {
const data = await todoService.getTodos();

setTodos(data);
} catch (err) {
setErrorMessage(Errors.UnableToLoad);
}
})();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better, what do u think?

Suggested change
(async () => {
try {
const data = await todoService.getTodos();
setTodos(data);
} catch (err) {
setErrorMessage(Errors.UnableToLoad);
}
})();
const getAllTodos = async () => {
try {
const data = await todoService.getTodos();
setTodos(data);
} catch (err) {
setErrorMessage(Errors.UnableToLoad);
}
}
getAllTodos();

src/App.tsx Outdated
Comment on lines 45 to 113
const onAddTodo = async (todoTitle: string) => {
setTempTodo({
id: 0,
title: todoTitle,
completed: false,
userId: todoService.USER_ID,
});
try {
const newTodo = await todoService.addTodo({
title: todoTitle,
completed: false,
});

setTodos(prev => [...prev, newTodo]);
} catch (err) {
setErrorMessage(Errors.UnableToAdd);
inputAddRef?.current?.focus();
throw err;
} finally {
setTempTodo(null);
}
};

const onRemoveTodo = async (todoId: number) => {
setLoadingTodoIds(prev => [...prev, todoId]);
try {
await todoService.deleteTodo(todoId);

setTodos(prev => prev.filter(todo => todo.id !== todoId));
} catch (err) {
setErrorMessage(Errors.UnableToDelete);
inputAddRef?.current?.focus();
throw err;
} finally {
setLoadingTodoIds(prev => prev.filter(id => id !== todoId));
}
};

const onUpdateTodo = async (todoToUpdate: Todo) => {
setLoadingTodoIds(prev => [...prev, todoToUpdate.id]);
try {
const updatedTodo = await todoService.updateTodo(todoToUpdate);

setTodos(prev =>
prev.map(todo => (todo.id === updatedTodo.id ? updatedTodo : todo)),
);
} catch (err) {
setErrorMessage(Errors.UnableToUpdate);
throw err;
} finally {
setLoadingTodoIds(prev => prev.filter(id => id !== todoToUpdate.id));
}
};

const onToggleAll = async () => {
if (todosActiveNumber > 0) {
todos
.filter(todo => !todo.completed)
.forEach(item => onUpdateTodo({ ...item, completed: true }));
} else {
todos.forEach(todo => onUpdateTodo({ ...todo, completed: false }));
}
};

const onClearCompleted = async () => {
const completedTodo = todos.filter(todo => todo.completed);

completedTodo.forEach(todo => onRemoveTodo(todo.id));
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need useCallback for these cases?

src/App.tsx Outdated
Comment on lines 115 to 124
const filteredTodos = todos.filter(todo => {
switch (filteredField) {
case FilterType.Active:
return !todo.completed;
case FilterType.Completed:
return todo.completed;
default:
return todos;
}
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. move this function to separate component and use useMemo for variable
  2. or just use useMemo

1 is better option

src/App.tsx Outdated
Comment on lines 145 to 164
{(todos.length > 0 || tempTodo) && (
<>
<TodoList
todos={filteredTodos}
onRemoveTodo={onRemoveTodo}
onUpdateTodo={onUpdateTodo}
loadingTodoIds={loadingTodoIds}
tempTodo={tempTodo}
/>
<Footer
todos={todos}
filteredField={filteredField}
setFilteredField={setFilteredField}
activeTodo={todosActiveNumber}
onClearCompleted={onClearCompleted}
/>
</>
)}
</div>
<ErrorNotification

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. use !! to convert value to boolean type
  2. don't forget about indents
Suggested change
{(todos.length > 0 || tempTodo) && (
<>
<TodoList
todos={filteredTodos}
onRemoveTodo={onRemoveTodo}
onUpdateTodo={onUpdateTodo}
loadingTodoIds={loadingTodoIds}
tempTodo={tempTodo}
/>
<Footer
todos={todos}
filteredField={filteredField}
setFilteredField={setFilteredField}
activeTodo={todosActiveNumber}
onClearCompleted={onClearCompleted}
/>
</>
)}
</div>
<ErrorNotification
{(!!todos.length || tempTodo) && (
<>
<TodoList
todos={filteredTodos}
onRemoveTodo={onRemoveTodo}
onUpdateTodo={onUpdateTodo}
loadingTodoIds={loadingTodoIds}
tempTodo={tempTodo}
/>
<Footer
todos={todos}
filteredField={filteredField}
setFilteredField={setFilteredField}
activeTodo={todosActiveNumber}
onClearCompleted={onClearCompleted}
/>
</>
)}
</div>
<ErrorNotification

src/components/Header/Header.tsx Outdated Show resolved Hide resolved
Comment on lines +42 to +50
useEffect(() => {
inputRef?.current?.focus();
}, [todosLength, inputRef]);

useEffect(() => {
if (!isInputDisabled) {
inputRef?.current?.focus();
}
}, [isInputDisabled, inputRef]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks strange

Suggested change
useEffect(() => {
inputRef?.current?.focus();
}, [todosLength, inputRef]);
useEffect(() => {
if (!isInputDisabled) {
inputRef?.current?.focus();
}
}, [isInputDisabled, inputRef]);
useEffect(() => {
inputRef?.current?.focus();
}, [todosLength, inputRef]);
useEffect(() => {
if (!isInputDisabled) {
inputRef?.current?.focus();
}
}, [isInputDisabled, inputRef]);


return (
<header className="todoapp__header">
{todosLength !== 0 && (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this syntax for all the cases(more readable)

Suggested change
{todosLength !== 0 && (
{!!todosLength && (

Comment on lines 1 to 28
// const updateTodo = (itemId, updatedTitle) => {
// const unmodifiedTodo = todos.find(todo => todo.id === itemId);

// if (!unmodifiedTodo) {
// endChaning(itemId);

// return;
// }

// if (updatedTitle === unmodifiedTodo.title) {
// return;
// }

// const updatedTodo = {
// ...unmodifiedTodo,
// title: updatedTitle !== null ? updatedTitle.trim() : unmodifiedTodo.title,
// completed:
// updatedTitle === null
// ? !unmodifiedTodo.completed
// : unmodifiedTodo.completed,
// };

// if (updatedTitle !== null && updatedTodo.title === '') {
// return deleteTodo(itemId);
// }

// setTodos(prev =>
// prev.map(todo => (todo.id === itemId ? updatedTodo : todo)),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unnecessary comments from ur project

@@ -0,0 +1,46 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
const BASE_URL = 'https://mate.academy/students-api';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move all the CONSTANTS to separate directory - constants

Copy link

@StasSokolov1 StasSokolov1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a masterpiece 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants